﻿using System;
using Microsoft.Xna.Framework;

namespace XNAFight.Characters.Core
{
    public class Jump
    {
        #region CHAMPS

        private Vector2 poussee;
        private float force_gravite;
        private Vector2 translation;
        private int current_frame;

        private float racine_de_deux;
        private float racine_force_gravite;

        #endregion

        #region PROPRIETES

        public Vector2 Translation
        {
            get { return translation; }
        }
        public Vector2 Poussee
        {
            get { return poussee; }
            set { poussee = value; }
        }
        public float Gravite
        {
            get { return force_gravite; }
            set
            {
                force_gravite = value;
                racine_force_gravite = (float)Math.Sqrt(force_gravite);
            }
        }
        public int FrameDuration
        {
            get { return getFrameDuration(); }
        }

        #endregion

        #region INITIALISATION

        public Jump(Vector2 _poussee, float _force_gravite)
        {
            poussee = _poussee;
            force_gravite = _force_gravite;
            translation = new Vector2();

            racine_de_deux = (float)Math.Sqrt(2);
            racine_force_gravite = (float)Math.Sqrt(force_gravite);
        }

        public void init(bool look_right)
        {
            current_frame = 0;
            if (look_right)
                translation.X = poussee.X;
            else
                translation.X = poussee.X * (-1);
        }

        #endregion


        public void update()
        {
            translation.Y = (int)((current_frame * racine_force_gravite * racine_de_deux) - poussee.Y);
            current_frame++;
        }

        public int getFrameDuration()
        {
            int frame_duration = 0;
            int deplacementY = 0;
            do
            {
                deplacementY += (int)((frame_duration * racine_force_gravite * racine_de_deux) - poussee.Y);
                frame_duration++;
            }
            while (deplacementY < 0);
            return frame_duration;
        }
    }
}
